package com.briup.mt.service.impl;

import com.briup.mt.bean.Module;
import com.briup.mt.bean.ModuleExample;
import com.briup.mt.dao.ModuleMapper;
import com.briup.mt.service.IModuleService;
import io.swagger.annotations.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.security.PrivateKey;
import java.util.Date;
import java.util.List;

@Service
public class ModuleServiceImpl implements IModuleService {

    @Resource
    private ModuleMapper moduleMapper;

    @Override
    public List<Module> query(String name, String algorithm, String introduce) {

        ModuleExample example=new ModuleExample();
        if(!StringUtils.isEmpty(name)){
            example.createCriteria().andNameLike("%"+name+"%");
        }
        if(!StringUtils.isEmpty(algorithm)){
            example.createCriteria().andAlgorithmEqualTo(algorithm);
        }
        if(!StringUtils.isEmpty(introduce)){
            example.createCriteria().andIntroduceLike("%"+introduce+"%");
        }
        List<Module> list = moduleMapper.selectByExample(example);
        return list;
    }

    @Override
    public void deleteById(long id) {
        moduleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Module module) {
        //如果Module中不存在此id，表示插入; 否则表示更新
        if(module.getId() == null){
            //初始化操作
            module.setCreateTime(new Date().getTime());
            moduleMapper.insert(module);
        }
        else{
            moduleMapper.updateByPrimaryKey(module);
        }
    }

}
